package com.len.util;


import com.alibaba.fastjson.JSONObject;
import com.len.base.CurrentUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * @author zhuxiaomeng
 * @date 2017/12/4.
 * @email 154040976@qq.com
 *
 * 管理工具类
 */
public class CommonUtil {

  /**
   * 获取当前用户
   */
  public static CurrentUser getUser() {
    org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    return (CurrentUser) session.getAttribute("currentUser");
  }

  public static String getTranslateResult(String query) throws IOException {
    String appid = "20240910002146647";
    String key = "h9MTB4OshL8zpVQbyaYP";
    String salt = UUID.randomUUID().toString().replace("-", "");
    String sign = Md5Util.getMD5(appid+query+salt+key);
    //越南
    String result = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="+ URLEncoder.encode(query,"UTF-8")+"&from=zh&to=vie&appid="+appid+"&salt="+salt+"&sign="+sign;
    String response = HttpUtils.get(result);
    System.out.println(response);
    JSONObject jsonObject = JSONObject.parseObject(response);
    String yuenan = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst");
    //英语
    result = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="+URLEncoder.encode(query,"UTF-8")+"&from=zh&to=en&appid="+appid+"&salt="+salt+"&sign="+sign;
    response = HttpUtils.get(result);
    jsonObject = JSONObject.parseObject(response);
    String yingyu = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst");

    JSONObject resultJson = new JSONObject();
    resultJson.put("zh",query);
    resultJson.put("vie",yuenan);
    resultJson.put("en",yingyu);

    return resultJson.toJSONString();
  }

  public static String getZhResult(String jsonStr) {
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    return jsonObject.getString("zh");
  }

}

  /**
   * 获取权限
   * @return
   */
  /*public static List<SysPermission> getPermission(){
    SysUser user=CommonUtil.getUser();
    if(user!=null){

    }
  }

}
*/